package com.lanou3g.musicenjoy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.entity.Mp3Info;
import com.lanou3g.musicenjoy.utils.CharacterParser;
import com.lanou3g.musicenjoy.utils.PinyinComparator;
import com.squareup.picasso.Picasso;

import java.util.Collections;
import java.util.List;

class ViewContainer{

    public TextView music_title;
    public TextView music_artist;
    public TextView music_duration;
    public TextView dialog;
}

public class MyListViewAdapter extends BaseAdapter {

    private Context context;        //上下文对象引用
    private List<Mp3Info> mp3Infos = null;   //存放Mp3Info引用的集合
    private Mp3Info mp3Info;        //Mp3Info对象引用
    private int pos = -1;           //列表位置
    private ViewContainer vc;
    private CharacterParser characterParser;
    private PinyinComparator pinyinComparator;



    public MyListViewAdapter(Context context,List<Mp3Info> mp3Infos){
        this.context = context;
        this.mp3Infos = mp3Infos;
        // 实例化汉字转拼音类
        characterParser = CharacterParser.getInstance();

        pinyinComparator = new PinyinComparator();
        Log.i("1111111",mp3Infos+"");
        Log.i("000", mp3Infos.get(0).getTitle() + ".....");
        setFirstLetter();
    }


    @Override
    public int getCount() {
        return mp3Infos != null && mp3Infos.size() > 0 ? mp3Infos.size() : 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // final PhoneModel mContent = mp3Info.get(position);
        vc = null;
        if(convertView == null){
            vc = new ViewContainer();
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.music_list_item_layout, null);

            vc.music_title = (TextView)convertView.findViewById(R.id.music_title);
            vc.music_artist = (TextView)convertView.findViewById(R.id.music_artist);
            vc.music_duration = (TextView)convertView.findViewById(R.id.music_duration);
            vc.dialog= (TextView) convertView.findViewById(R.id.catalog);

            convertView.setTag(vc);
        }
        else{
            vc = (ViewContainer)convertView.getTag();
        }
        mp3Info = mp3Infos.get(position);
        Log.i("tupian",mp3Info.getAlbum() + "");
        //Picasso.with(context).load(mp3Infos.get(position).getAlbum()).into(vc.music_heard);
        vc.music_title.setText(mp3Info.getTitle());         //显示标题
        vc.music_artist.setText(mp3Info.getArtist());       //显示艺术家
        vc.music_duration.setText(String.valueOf(formatTime(mp3Info.getDuration()))); //显示长度


        // 根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);
        
        // 如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            vc.dialog.setVisibility(View.VISIBLE);
            vc.dialog.setText(mp3Info.getSortLetters());
        } else {
            vc.dialog.setVisibility(View.GONE);
        }

        return convertView;

    }

    public void setFirstLetter(){
        for(Mp3Info mp3Info:mp3Infos){
            String pinyin = characterParser.getSelling(mp3Info.getTitle());
            String sortString = pinyin.substring(0, 1).toUpperCase();

            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                mp3Info.setSortLetters(sortString.toUpperCase());
            } else {
                mp3Info.setSortLetters("#");
            }

        }
            // 根据a-z进行排序源数据
            Collections.sort(mp3Infos, pinyinComparator);
    }


    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        // 放回第0个位置的char值
        Log.i("xxx", mp3Infos + "");
        return mp3Infos.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = mp3Infos.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    /**
     * 提取英文的首字母，非英文字母用#代替。
     *
     * @param str
     * @return
     */
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Object getItem(int position) {
        return mp3Infos != null && mp3Infos.size() > 0 ? mp3Infos.get(position) :null;
    }

    public static String formatTime(Long time){                     //将歌曲的时间转换为分秒的制度
        String min = time / (1000 * 60) + "";
        String sec = time % (1000 * 60) + "";

        if(min.length() < 2)
            min = "0" + min;
        switch (sec.length()){
            case 4:
                sec = "0" + sec;
                break;
            case 3:
                sec = "00" + sec;
                break;
            case 2:
                sec = "000" + sec;
                break;
            case 1:
                sec = "0000" + sec;
                break;
        }
        return min + ":" + sec.trim().substring(0,2);
    }
}
